package com.surfilter.admin.project.system.controller;

import com.surfilter.admin.common.utils.SecurityUtils;
import com.surfilter.admin.common.utils.poi.ExcelUtil;
import com.surfilter.admin.framework.aspectj.lang.annotation.Log;
import com.surfilter.admin.framework.aspectj.lang.enums.BusinessType;
import com.surfilter.admin.framework.web.controller.BaseController;
import com.surfilter.admin.framework.web.domain.AjaxResult;
import com.surfilter.admin.framework.web.page.TableDataInfo;
import com.surfilter.admin.project.system.domain.SysDictData;
import com.surfilter.admin.project.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典数据信息
 * 
 *
 */
@Api(tags = "字典数据信息")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @ApiOperation(value = "查询字典数据信息", notes = "查询字典数据信息", response = TableDataInfo.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictData", value = "字典数据信息", required = true, paramType = "query",
            dataType = "SysDictData")})
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @ApiOperation(value = "导出数据字典信息", notes = "导出数据字典信息", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictData", value = "字典数据信息", required = true, paramType = "query",
            dataType = "SysDictData")})
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @GetMapping("/export")
    public AjaxResult export(SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @ApiOperation(value = "查询字典数据详细", notes = "查询字典数据详细", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictCode", value = "数据字典信息Id", required = true, paramType = "query",
            dataType = "Long")})
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation(value = "根据字典类型查询字典数据信息", notes = "根据字典类型查询字典数据信息", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictType", value = "数据字典信息类型", required = true, paramType = "query",
            dataType = "String")})
    @GetMapping(value = "/dictType/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        return AjaxResult.success(dictDataService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典类型
     */
    @ApiOperation(value = "新增字典类型", notes = "新增字典类型", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictData", value = "数据字典信息", required = true, paramType = "query",
            dataType = "SysDictData")})
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation(value = "修改保存字典类型", notes = "修改保存字典类型", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictData", value = "数据字典信息", required = true, paramType = "query",
            dataType = "SysDictData")})
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @ApiOperation(value = "删除字典类型", notes = "删除字典类型", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictCodes", value = "数据字典信息id集合", required = true, paramType = "query",
            dataType = "Long[]")})
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
    }
}
